﻿using FunArts.EasyDevOps.Rcl.Service;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunArts.EasyDevOps.Client.Service.Impl
{
    /// <summary>
    /// 服务端平台读取文件实现
    /// </summary>
    public class LocalFileService : ILocalFileService
    {
        public byte[] fileContent { get; set; }
        public string fileName { get; set; }
        public JSRuntime jSRuntime { get; set; }


        public Task<KeyValuePair<string, List<string>>> OpenDir()
        {
            throw new NotImplementedException("当前平台不支持打开文件夹");
        }

        public async Task<KeyValuePair<string, byte[]>> OpenFile(string fileName = "")
        {
            await jSRuntime.InvokeVoidAsync("openFileDialog", "HandleFileSelected");
            return new KeyValuePair<string, byte[]>(fileName, fileContent);
        }

        [JSInvokable]
        public void HandleFileSelected(byte[] fileData, string fileName)
        {
            fileContent=fileData;
            fileName = fileName.Trim();
        }
    }
}
